<script setup>
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'

	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
	import {
		computed,
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import {
		getUserProfile
	} from '@/api/system/user'
	import {
		addPersonnelApply
	} from '@/api/attendance'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import approvalProcessState from '@/store/modules/approvalProcess'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'
	const title = '入职申请'
	const onboardingPostRef = ref(null)
	const onboardingPost = ref(null)
	const useDateRef = ref(null)
	const onboardingList = ref([{
		entryName: '',
		deptName: '',
		deptId: '',
		posts: '',
		date: '',
		probationPeriodDuration: '',
		rules: '',
	}, ])

	const onboardingPostFn = () => {
		onboardingPostRef.value.open()
	}
	const onboardingPostColumns = [
		['是', '否']
	]
	const onboardingPostConfirm = (e) => {
		onboardingPost.value = e.value[0]
	}
	// const formatter = (type, value) => {
	// 	if (type === 'year') {
	// 		return `${value}年`
	// 	}
	// 	if (type === 'month') {
	// 		return `${value}月`
	// 	}
	// 	if (type === 'day') {
	// 		return `${value}日`
	// 	}
	// 	if (type === 'hour') {
	// 		return `${value}时`
	// 	}
	// 	if (type === 'minute') {
	// 		return `${value}分`
	// 	}
	// 	return value
	// }
	// const useDateFn = () => {
	// 	useDateRef.value.open()
	// }
	// let useDatePicker = dayjs()
	// const useDateConfirm = (obj) => {
	// 	onboardingList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
	// }
	const canplayFn = (index) => {
		onboardingList.value.push({
			entryName: '',
			deptName: '',
			deptId: '',
			date: '',
			posts: '',
			probationPeriodDuration: '',
			rules: '',
		})
	}
	const deleteFn = (index) => {
		onboardingList.value.splice(index, 1)
	}

	const userId = ref(null)
	const deptId = ref(null)
	const getUserProfileFn = async () => {
		let {
			code,
			data
		} = await getUserProfile()
		if (code === 200) {
			userId.value = data.userId
			deptId.value = data.deptId
		}
	}
	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=入职申请&apiName=人事',
		})
	}
	let attchment = ref([])
	const getDesignDrawingsEmit = (emit) => {
		attchment.value = emit
	}
	const clickSave = async () => {
		onboardingList.value.onboardingPost = onboardingPost.value
		if (isObjectValuesNotEmpty(onboardingList.value)) {
			let params = {
				trialPost: onboardingPost.value,
				details: JSON.stringify(onboardingList.value),
				ruleId: 'ruzhi',
				userId: userId.value,
				deptId: deptId.value,
				type: '入职申请',
				attchment: JSON.stringify(attchment.value),
				approve: approvalProcessState.state.approvalProcess,
			}
			let {
				code
			} = await addPersonnelApply(params)
			if (code === 200) {
				uni.showToast({
					title: '提交成功',
					duration: 1500,
					icon: 'success',
				})

				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}

	}

	onLoad(() => {
		getUserProfileFn()
	})

	const onboardingListIndex = ref(0)
	const withSealFn = (i) => {
		onboardingListIndex.value = i
		uni.navigateTo({
			url: '/components/OptionDeptPerson/deptName',
		})
	}

	onShow(() => {
		if (optionDeptPerson.state.optionDept) {
			onboardingList.value[onboardingListIndex.value].deptName = optionDeptPerson.state.optionDept.label
			onboardingList.value[onboardingListIndex.value].deptId = optionDeptPerson.state.optionDept.depId
		}
	})
</script>

<template>
	<view>
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" draggable="false" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView" />
		</HeaderNavTabarSvg>

<!-- 		<view class="picker-content" @tap="onboardingPostFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired"> 分公司副总及以上</text>
				<view class="info-list-right">
					<view v-if="onboardingPost" class="uni-input">{{ onboardingPost }} </view>
					<view v-else class="uni-inputs">(入职岗位)</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view> -->
		
		<view class="picker-content">
		  <view class="measurement-info-list">
		    <text class="info-list-left isrequired">分公司副总及以上</text>
		    <view class="info-list-right">
		      <view class="uni-input"
		        >
							<uv-radio-group v-model="onboardingPost" placement="row">
								<uv-radio name="是" label="是"></uv-radio>
								<uv-radio name="否" activeColor="red" label="否"></uv-radio>
							</uv-radio-group>
		      </view>
		    </view>
		  </view>
		</view>

		<view v-for="(item, index) in onboardingList" :key="index" class="onboarding-box">
			<view class="box-header">
				<text>明细{{ index + 1 }}</text>
				<view>
					<text class="copyBtn" @tap="canplayFn">复制</text>
					<text v-show="index !== 0" class="deleteBtn" @tap="deleteFn">删除</text>
				</view>
			</view>

			<view class="box-main">
				<view class="picker-content">
					<view class="measurement-info-list">
						<text class="info-list-left isrequired">入职人姓名</text>
						<view class="info-list-right">
							<view class="info-content">
								<input v-model="item.entryName" adjust-position class="uni-input" placeholder="请输入" type="text" />
							</view>
						</view>
					</view>
				</view>
				<view class="picker-content" @tap="withSealFn(index)">
					<view class="measurement-info-list">
						<text class="info-list-left isrequired">部门</text>
						<view class="info-list-right">
							<view class="info-content">
								<view v-if="item.deptName" class="uni-input">{{ item.deptName }} </view>
								<view v-else style="color: #86909c; font-size: 28rpx">请选择</view>
							</view>
						</view>
					</view>
				</view>
				<view class="picker-content">
					<view class="measurement-info-list">
						<text class="info-list-left isrequired">职位</text>
						<view class="info-list-right">
							<view class="info-content">
								<input v-model="item.posts" adjust-position class="uni-input" placeholder="请输入" type="text" />
							</view>
						</view>
					</view>
				</view>
				     <view class="picker-content">
          <view class="measurement-info-list">
            <text class="info-list-left isrequired">入职日期</text>
            <view class="info-list-right">
              <view class="info-content">
                <input
                  v-model="item.date"
                  adjust-position
                  class="uni-input"
                  placeholder="请输入"
                  type="text"
                />
              </view>
            </view>
          </view>
        </view>

				<view class="picker-content">
					<view class="measurement-info-list">
						<text class="info-list-left isrequired">试用期时长</text>
						<view class="info-list-right">
							<view class="info-content">
								<input v-model="item.probationPeriodDuration" adjust-position class="uni-input" placeholder="填写入职约定的试用期"
									type="text" />
							</view>
						</view>
					</view>
				</view>

				<view class="textarea-box">
					<text class="info-list-left isrequired">试用期工资及工资结构</text>
					<textarea v-model="item.rules" class="textinfo-list" placeholder="请输入"></textarea>
				</view>
			</view>
		</view>

		<uv-picker ref="onboardingPostRef" :columns="onboardingPostColumns" @confirm="onboardingPostConfirm"></uv-picker>

		<!--    说明附件-->
		<DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" />
		<!--    审批流程-->
		<AuditApplicationSteps :position="onboardingPost" :ruleId="'ruzhi'" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.onboarding-box {
		.box-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 32rpx;

			.copyBtn {
				margin-right: 32rpx;
				display: inline-block;
				color: #165dff;
			}

			.deleteBtn {
				color: #165dff;
			}
		}
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.uni-input {
		text-align: right;
		font-size: 28rpx;
		.uv-radio-label--left{
			margin-right: 10rpx;
		}
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.card-list {
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.list {
				box-shadow: 0 4rpx 16rpx 0 #0000000d;
				box-sizing: border-box;
				width: 100%;
				background: #fff;
				padding: 16rpx 24rpx;
				border-radius: 8rpx;
				margin-bottom: 10rpx;

				h4 {
					color: #1d2129;
					font-size: 32rpx;
					font-weight: 400;
				}

				p {
					color: #86909c;
					font-size: 24rpx;
					font-weight: 400;
					margin-top: 10rpx;
				}
			}
		}
	}

	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}
</style>